package cn.yzx.pethome.system.verifycode.controller;


import cn.yzx.pethome.constants.BaseConstants;
import cn.yzx.pethome.system.user.domain.User;
import cn.yzx.pethome.system.user.service.IUserService;
import cn.yzx.pethome.system.verifycode.domain.Vcode;
import cn.yzx.pethome.utils.AjaxResult;
import cn.yzx.pethome.utils.StrUtils;
import cn.yzx.pethome.utils.VerifyCodeUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/vcode")
//这里是说明你这个类是干什么用的
@Api(value = "验证码的API",description="验证码的功能")
public class VerifyCodeController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserService userService;

    @GetMapping("/imgCode/{key}")
    public AjaxResult getImgCode(@PathVariable String key){
        try {
            //生成随机验证码
            String code = StrUtils.getRandomString(6);
            //存入redis。有效期3分钟
            redisTemplate.opsForValue().set(key,code,3, TimeUnit.MINUTES);
            String base64Img = VerifyCodeUtils.VerifyCode(110, 40, code);
            return AjaxResult.success().setData(base64Img);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络异常，请联系管理员");
        }
    }

    @PostMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody Vcode vcode){
        String phone = vcode.getPhone();

        //手机号是否为空
        if (phone == null || "".equals(phone.trim())){
            return AjaxResult.error().setMsg("手机号不能为空");
        }
        //判断手机号是否注册
        User user = userService.findByPhone(phone);
        if (user == null){
            return AjaxResult.error().setMsg("该手机号还未被注册，请注册后登录");
        }
        //生成验证码
        String phoneCode = StrUtils.getRandomString(6);

        //获取存时间的key
        String keyTime = phone + BaseConstants.Login.LOGIN_TIME;
        //获取存要验证码的key
        String key = phone + BaseConstants.Login.LOGIN;
        //获取上次发送验证码的时间
        Object o1 = redisTemplate.opsForValue().get(keyTime);
        //判断是是否有值
        //为null表示第一次发送,不为null判断时间是否大于60
        if (o1 == null){
            //获取当前时间戳
            long startTime = System.currentTimeMillis();
            //将验证码存入redis
            redisTemplate.opsForValue().set(key,phoneCode,BaseConstants.Login.LOGIN_TIME_OUT, TimeUnit.MINUTES);
            //将时间存入redis
            redisTemplate.opsForValue().set(keyTime,startTime,BaseConstants.Login.LOGIN_TIME_OUT, TimeUnit.MINUTES);
        }else {
            long sTime = Long.parseLong(o1.toString());
            //获取当前时间
            long endTime = System.currentTimeMillis();
            if(endTime-sTime<60000){
                return AjaxResult.error().setMsg("请勿重复获取验证码");
            }else {
                //将上次验证码重新存入redis
                phoneCode = (String) redisTemplate.opsForValue().get(key);
                //重置时间
                long startTime = System.currentTimeMillis();
                redisTemplate.opsForValue().set(key,phoneCode,BaseConstants.Login.LOGIN_TIME_OUT,TimeUnit.MINUTES);
                redisTemplate.opsForValue().set(keyTime,startTime,BaseConstants.Login.LOGIN_TIME_OUT,TimeUnit.MINUTES);
            }
        }
        //发送短信验证码
//        SendMsgUtil.sendMassage(msg_Uid,msg_Key,phone,phoneCode);
        System.out.println(phoneCode);
        return AjaxResult.success().setMsg("短信发送成功");
    }
}
